package com.gugee.tiktok.common.model.dal.hashtag.impl;

import com.gugee.tiktok.common.model.dal.hashtag.AwemeHashtagExtRepository;
import com.gugee.tiktok.common.model.mysql.AwemeHashtag;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: luolifeng
 * Date: 2020-11-12 21:20
 */
public class AwemeHashtagExtRepositoryImpl implements AwemeHashtagExtRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public void batchSaveAwemeHashtag(List<AwemeHashtag> awemeHashtags) {
        long seconds = DateTimeUtils.currentTimeSecond();
        /**
         * todo 收录标签，若想解耦，可将hid发送到消息队列批量消费
         */
        jdbcTemplate.batchUpdate(
                "INSERT INTO `tiktok`.`aweme_hashtag`" +
                        "(`id`, `hid`," +
                        " `create_time`, " +
                        "`update_time`, `create_by`, `update_by`, `aweme_id`, `hashtag_name`, `is_commerce`, `uid`)" +
                        " VALUES(?,?,?,?,?,?,?,?,?,?)"
                , new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        AwemeHashtag hashTag = awemeHashtags.get(i);
                        preparedStatement.setLong(1, 0);
                        preparedStatement.setLong(2, hashTag.getHid());
                        preparedStatement.setLong(3,hashTag.getCreateTime());
                        preparedStatement.setLong(4, seconds);
                        preparedStatement.setString(5, "batch save");
                        preparedStatement.setString(6, "batch save");
                        preparedStatement.setLong(7,hashTag.getAwemeId());
                        preparedStatement.setString(8, hashTag.getHashtagName());
                        preparedStatement.setBoolean(9,hashTag.isCommerce());
                        preparedStatement.setLong(10,hashTag.getUid());
                    }

                    @Override
                    public int getBatchSize() {
                        return awemeHashtags.size();
                    }
                });
    }

}
